local diaodu = fk.CreateSkill{
    name = "ld__diaodu",
}

local H = require "packages.ol_hegemony.util"

diaodu:addEffect(fk.CardUsing,{
    anim_type = "drawcard",
    can_trigger = function(self, event, target, player, data)
        if not player:hasSkill(diaodu.name) then return false end
        return H.compareKingdomWith(target, player) and data.card.type == Card.TypeEquip and (player:hasShownSkill(diaodu.name) or player == target)
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        return room:askToSkillInvoke(target,{
            skill_name = diaodu.name,
            prompt = "#ld__diaodu-invoke",
        })
    end,
    on_use = function(self, event, target, player, data)
      target:drawCards(1, diaodu.name)
    end,
})

diaodu:addEffect(fk.EventPhaseStart,{
    anim_type = "drawcard",
    can_trigger = function(self, event, target, player, data)
        if not player:hasSkill(diaodu.name) then return false end
        return target == player and target.phase == Player.Play and table.find(player.room.alive_players, function(p)
            return H.compareKingdomWith(p, player) and #p:getCardIds("e") > 0 end)
      end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local targets = table.filter(room.alive_players, function(p) return H.compareKingdomWith(p, player) and #p:getCardIds("e") > 0 end)
          if #targets == 0 then return false end
          local to = room:askToChoosePlayers(player,{
            targets = targets,
            min_num = 1,
            max_num = 1,
            prompt = "#ld__diaodu-choose",
            skill_name = diaodu.name,
            cancelable = true,
          })
          if #to > 0 then
            event:setCostData(self, {tos = to })
            return true
        end
      end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      local to = event:getCostData(self).tos[1]
      local cid = room:askToChooseCard(player,{
        target = to,
        flag = "e",
        skill_name = diaodu.name,
      })
      room:obtainCard(player, cid, true, fk.ReasonPrey)
      if not table.contains(player:getCardIds("h"), cid) then return false end
      local card = Fk:getCardById(cid)
      if player.dead then return false end
      local targets = table.filter(room.alive_players, function(p) return p ~= player and p ~= to end)
      local tos = room:askToChoosePlayers(player,{
        targets = targets,
        min_num = 1,
        max_num = 1,
        prompt = "#ld__diaodu-give:::" .. card:toLogString(),
        skill_name = diaodu.name,
        cancelable = true,
      })
      if #tos > 0 then
        room:moveCardTo(card, Card.PlayerHand, tos[1], fk.ReasonGive, diaodu.name, nil, true, player)
      end
    end,
})

Fk:loadTranslationTable{
    ["ld__diaodu"] = "调度",
    [":ld__diaodu"] = "①当每回合有与你势力相同的角色使用装备牌时，其可摸一张牌。②出牌阶段开始时，你可获得一名与你势力相同的角色装备区里的一张牌，然后你可以将此牌交给另一名角色。",

    ["#ld__diaodu-invoke"] = "调度：你可摸一张牌",
    ["#ld__diaodu-choose"] = "调度：你可获得与你势力相同的一名角色装备区里的一张牌",
    ["#ld__diaodu_draw"]="调度",
    ["#ld__diaodu-give"] = "调度：将%arg交给另一名角色",

    ["$ld__diaodu1"] = "诸军兵器战具，皆由我调配。",
    ["$ld__diaodu2"] = "甲胃兵器，按我所说之法分发。",
}

return diaodu